﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using DevelopersTestPad.AppFrame;
using FastColoredTextBoxNS;

namespace DevelopersTestPad.Worksheets.DotNet
{
    /// <summary>
    /// This is a .NET source editor.
    /// </summary>
    internal partial class SourceEditor : UserControl
    {
        #region Private Fields
        private Worksheet _worksheet;
        private bool _initializing;
        #endregion

        public SourceEditor()
        {
            InitializeComponent();
        }

        /// <summary>
        /// The worksheet.
        /// </summary>
        public Worksheet Worksheet
        {
            get { return _worksheet; }
            set
            {
                _worksheet = value;

                if (_worksheet != null)
                {
                    Initialize();
                }
            }
        }

        /// <summary>
        /// Gets the text.
        /// </summary>
        public new string Text { get { return fastColoredTextBox.Text; } }

        /// <summary>
        /// Gets the selected text.
        /// </summary>
        public string SelectedText { get { return fastColoredTextBox.SelectedText; } }

        /// <summary>
        /// Initializes the .NET source editor.
        /// </summary>
        private void Initialize()
        {
            try
            {
                _initializing = true;

                AppContext.Current.AppNotificationReceived += Current_AppNotificationReceived;

                SetLanguage();
                
                fastColoredTextBox.Text = Worksheet.TextData;
            }
            finally
            {
                _initializing = false;
            }
        }

        private void Current_AppNotificationReceived(object sender, AppNotificationEventArgs e)
        {
            if (e.Type == AppNotificationType.WorksheetTypeChanged)
            {
                SetLanguage();   
            }
        }

        private void SetLanguage()
        {
            switch (Worksheet.Type)
            {
                case WorksheetType.CSharpScript:
                    fastColoredTextBox.Language = Language.CSharp;
                    break;
                case WorksheetType.VisualBasicScript:
                    fastColoredTextBox.Language = Language.VB;
                    break;
                default:
                    throw new ArgumentException("worksheetType");
            }            

            
        }

        protected override void OnPaintBackground(PaintEventArgs e)
        {
            base.OnPaintBackground(e);

            Graphics g = e.Graphics;

            Rectangle rect = new Rectangle(2, 1, Width - 4, 1);

            LinearGradientBrush brush = new LinearGradientBrush(rect,
                                                                Color.FromArgb(255, 79, 146, 219),
                                                                Color.FromArgb(255, 79, 146, 219),
                                                                LinearGradientMode.ForwardDiagonal);

            g.FillRectangle(brush, rect);

            rect = new Rectangle(2, 1, 1, Height - 1);
            g.FillRectangle(brush, rect);

            rect = new Rectangle(2, Height - 1, Width - 4, 1);
            g.FillRectangle(brush, rect);

            rect = new Rectangle(Width - 3, 1, 1, Height - 1);
            g.FillRectangle(brush, rect);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            Invalidate();
        }

        /// <summary>
        /// Handles the TextChanged event.
        /// </summary>
        private new void TextChanged(object sender, TextChangedEventArgs e)
        {
            if (_initializing)
            {
                return;
            }
            
            switch (Worksheet.Status)
            {
                case WorksheetStatus.Initialized:
                    Worksheet.Status = WorksheetStatus.Dirty;
                    AppContext.Current.OnAppNotificationReceived(new AppNotificationEventArgs { Type = AppNotificationType.WorksheetStatusChanged });
                    break;
                case WorksheetStatus.Dirty:
                    // no need to change status
                    break;
                case WorksheetStatus.Clean:
                    Worksheet.Status = WorksheetStatus.Dirty;
                    AppContext.Current.OnAppNotificationReceived(new AppNotificationEventArgs { Type = AppNotificationType.WorksheetStatusChanged });
                    break;
            }

            Worksheet.TextData = fastColoredTextBox.Text;
        }

        #region ContextMenu

        private void contextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            mniCut.Enabled = !string.IsNullOrEmpty(fastColoredTextBox.SelectedText);
            mniCopy.Enabled = !string.IsNullOrEmpty(fastColoredTextBox.SelectedText);
            mniPaste.Enabled = Clipboard.ContainsText();
            mniClearAll.Enabled = !string.IsNullOrEmpty(fastColoredTextBox.Text);
        }

        /// <summary>
        /// Cut selected text into Clipboard.
        /// </summary>
        private void mniCut_Click(object sender, EventArgs e)
        {
            fastColoredTextBox.Cut();
        }

        /// <summary>
        /// Copy selected text into Clipboard.
        /// </summary>
        private void mniCopy_Click(object sender, EventArgs e)
        {
            fastColoredTextBox.Copy();
        }

        /// <summary>
        /// Paste text from Clipboard.
        /// </summary>
        private void mniPaste_Click(object sender, EventArgs e)
        {
            fastColoredTextBox.Paste();
        }

        /// <summary>
        /// Clear all text.
        /// </summary>
        private void mniClearAll_Click(object sender, EventArgs e)
        {
            fastColoredTextBox.Clear();
        }

        #endregion

    }
}
